﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Common;
using Core;
using MyCart.Models;

namespace MyCart.Controllers
{
    public class ProductApiController : BaseAPIController
    {
        private readonly IProductRepository _ProductRepository;
        private readonly ICategoryRepository _CategoryRepository;

        public ProductApiController(IProductRepository _productRepository,
                                ICategoryRepository _categoryRepository)
        {
            _ProductRepository = _productRepository;
            _CategoryRepository = _categoryRepository;
        }

        // GET api/products
        public IEnumerable<ProductView> Get()
        {
            var prodList = _ProductRepository.GetAll();
            var catIds = prodList.Select(p => p.CategoryId).Distinct();
            var catList = _CategoryRepository.GetByConditoin(c => catIds.Contains(c.ID));

            return prodList.Join(catList, p => p.CategoryId, c => c.ID, (p, c) => _GetViewModel(p, c));

        }

        // GET api/products/5
        public ProductView Get(long id)
        {
            Product prod = _ProductRepository.GetById(id);
            Category cat = _CategoryRepository.GetById(prod.CategoryId);
            return _GetViewModel(prod, cat);
        }

        // POST api/products
        public HttpResponseMessage Post(ProductView prodView)
        {
            if (prodView != null && ModelState.IsValid)
            {
                Product prod = _ProductRepository.GetById(prodView.ProductId) ?? new Product();
                prod.ProductName = prodView.ProductName;
                prod.Price = prodView.Price;
                prod.CategoryId = prodView.CategoryId;
                if (prodView.ProductId == 0)
                {
                    prod.CreatedBy = UserId;
                    prod.CreatedOn = DateTime.UtcNow;
                }
                prod.UpdatedBy = UserId;
                prod.UpdatedOn = DateTime.UtcNow;

                var resultId = _ProductRepository.AddUpdate(prod);

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, resultId);
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Please check the request..");
            }
        }

        // DELETE api/products/5
        public void Delete(long id)
        {
            _ProductRepository.Delete(id);
        }

        private ProductView _GetViewModel(Product prod, Category cat)
        {
            return new ProductView
            {
                ProductId = prod.ID,
                CategoryId = cat.ID,
                ProductName = prod.ProductName,
                CategoryName = cat.CategoryName,
                Price = prod.Price
            };
        }
    }
}